package controllers.wxcp;

import auth.WxCpAuth;
import auth.WxMpAuth;
import ext.wxcp.WeixinCpHelper;
import helper.GlobalConfig;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.cp.bean.WxCpMessage;
import me.chanjar.weixin.cp.bean.WxCpUser;
import models.admin.AdminUser;
import models.issue.Issue;
import models.system.Company;
import models.user.User;
import play.Logger;
import play.mvc.Controller;
import play.mvc.With;

import java.util.Date;
import java.util.List;

/**
 * 微信企业号默认页面.
 */
@With(WxCpAuth.class)
public class WxCpApplications extends Controller {

    public static void index() {
        AdminUser adminUser = WxCpAuth.currentUser();
        if(adminUser == null) {
            renderText("用户不能为空,请在管理后台绑定用户!");
        }
//        Company company = adminUser.organization;
//        if(company != null) {
//
//        }
        System.out.println("CP用户ID"+adminUser.id);
        List<Issue> issueList = Issue.findByDetailOprate(adminUser);
        if(issueList != null && issueList.size() > 5) {
            issueList = issueList.subList(0,5);
        }
        render(adminUser,issueList);
    }

    public static void bindWeiXinUser(String id) {
        if(id == null || "".equals(id.trim()) ) {
            String msg = "未找到用户ID";
            flash.error(msg);
            redirect("/wxcp/home");
        }
        AdminUser adminUser = AdminUser.findAdminUserById(Long.parseLong(id));
        if (adminUser == null) {
            String msg = "未找到相应用户";
            flash.error(msg);
            redirect("/wxcp/home");
        }
        WxCpUser wxCpUser = WxCpAuth.currentWxCpUser();
        if(wxCpUser == null) {
            String msg = "未找到可以绑定的用户";
            flash.error(msg);
            redirect("/wxcp/home");
        }
        adminUser.modifyAt = new Date();
        adminUser.cpUserId = wxCpUser.getUserId();
        adminUser.email = wxCpUser.getEmail();
        adminUser.phoneNo = wxCpUser.getMobile();
        adminUser.position = wxCpUser.getPosition();
        adminUser.name = wxCpUser.getName();
        adminUser.weixinId = wxCpUser.getWeiXinId();
        adminUser.save();
        String msg = "已绑定用户";
        flash.error(msg);
        redirect("/wxcp/home");
    }

    /**
     * 企业号消息推送的例子.
     */
    public static void pushMessage() {


        WxCpMessage.WxArticle article1 = new WxCpMessage.WxArticle();
        article1.setUrl("http://" + GlobalConfig.WEIXIN_BASE_DOMAIN + "/wxcp/home");
        //article1.setPicUrl("PIC_URL");
        article1.setDescription("内容说明，可自由拼接文字");
        article1.setTitle("标题1");


        WxCpMessage message = WxCpMessage.NEWS()
                .agentId(GlobalConfig.WXCP_WY_AGENT_ID) // 企业号应用ID
                .toUser("@all")
                .addArticle(article1)
                .build();
        try {
            WeixinCpHelper.getWxCpService().messageSend(message);
        } catch (WxErrorException e) {
            Logger.warn(e, "LOG100131: 发送文章失败:" + article1);
        }

    }
}
